package com.kay.car.util;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

public class DataUtil {
    public static JSONObject
    getPrice(Double[] numArray){
        System.out.println("-----------------");
        System.out.println("size:"+numArray.length);
        Arrays.sort(numArray);
        double median = numArray[numArray.length / 2];
        double sd = calculateSD(numArray);
        double min = median - 2 * sd;
        double max = median + 2 * sd;
        System.out.println("max:"+max);
        System.out.println("min:"+min);
        double sum = 0;
        int count = 0;
        for (double d:numArray) {
            if(d<=max&&d>=min){
                sum = sum + d;
                count ++;
            }
        }
        System.out.println("sum:"+sum);
        System.out.println("count:"+count);
        JSONObject json = new JSONObject();
        json.put("price",sum/count);
        json.put("count",count);
        return json;
    }

    public static double calculateSD(Double[] numArray) {
        double sum = 0.0, standardDeviation = 0.0;
        int length = numArray.length;

        for(double num : numArray) {
            sum += num;
        }

        double mean = sum / length;

        for(double num: numArray) {
            standardDeviation += Math.pow(num - mean, 2);
        }

        return Math.sqrt(standardDeviation/length);
    }

    public static void main(String[] args) {
//        Double[] a = {1.0,2.0,3,4,2,3,4,3,1,2,5,2,1,50,10,30};
//        System.out.println(getPrice(a));
    }
}
